{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49d25751",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "数据集 28*28的图片\n",
    "\n",
    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n",
    "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "2e50e600",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7f74ef473400>"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaAAAAGdCAYAAABU0qcq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Lq7uxUIBIymSg/5+fl6+umn1dbWZj2KmS+OAY6Pe02bNk2FhYVZeXysX79ehw8f1okTJxJ+fUsgENCtW7fU09OTsH22Hg9D7YfBVFRUSFJaHQ9pH6Dc3FzNmTNHjY2N8cf6+/vV2NioyspKw8nsXb9+Xe3t7SopKbEexUxZWZkCgUDC8RGNRnX69OlH/vi4fPmyrl27llXHh3NO69evV0NDg44fP66ysrKE5+fMmaOcnJyE46G1tVWXLl3KquPhQfthMOfOnZOk9DoerK+CGI733nvPeb1et3fvXvfPf/7TvfLKKy4/P9+Fw2Hr0cbUq6++6pqamlxHR4f729/+5qqrq11hYaG7evWq9Wgp1dvb686ePevOnj3rJLk333zTnT171v33v/91zjn329/+1uXn57tDhw658+fPuxdeeMGVlZW5zz//3Hjy5Lrffujt7XWvvfaaa2lpcR0dHe7YsWPu29/+tnvqqafczZs3rUdPmnXr1jm/3++amppcV1dXfN24cSO+zdq1a93UqVPd8ePH3ccff+wqKytdZWWl4dTJ96D90NbW5n7961+7jz/+2HV0dLhDhw65adOmuQULFhhPnigjAuScc2+//babOnWqy83NdfPmzXOnTp2yHmnMrVixwpWUlLjc3Fz3jW98w61YscK1tbVZj5VyJ06ccJLuWStXrnTODVyKvXXrVldcXOy8Xq+rqqpyra2ttkOnwP32w40bN9zChQvd5MmTXU5OjistLXVr1qzJuv9JG+zfX5Lbs2dPfJvPP//c/eQnP3Ff//rX3WOPPeaWLl3qurq67IZOgQfth0uXLrkFCxa4goIC5/V63ZNPPul+9rOfuUgkYjv4Xfh1DAAAE2n/HhAAIDsRIACACQIEADBBgAAAJggQAMAEAQIAmCBAAAATBAgAYIIAAQBMECAAgAkCBAAwQYAAACb+D/+rctgFJ4uyAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "m_x = np.loadtxt('../data/mnist_x',dtype=int)\n",
    "m_y = np.loadtxt('../data/mnist_y',dtype=int)\n",
    "\n",
    "plt.figure()\n",
    "plt.imshow(m_x[0].reshape(28,28), cmap=\"autumn\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "14684fcc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def distance(a,b):\n",
    "    return np.sqrt(np.sum(np.square(a-b)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c9860b6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 划分训练集和测试集\n",
    "ratio = 0.8\n",
    "split = int(len(m_x)*ratio)\n",
    "x_train, x_test = m_x[:split], m_x[split:]\n",
    "y_train, y_test = m_y[:split], m_y[split:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "419bbeed",
   "metadata": {},
   "outputs": [],
   "source": [
    "class KNN():\n",
    "    def __init__(self ,k):\n",
    "        self.k = k\n",
    "    def fit(self,x_train,y_train):\n",
    "        self.x_train = x_train\n",
    "        self.y_train = y_train\n",
    "    def get_knn_index(self,x):\n",
    "        dis = list(map(lambda a:distance(a,x), x_train))\n",
    "        knn_index = np.argsort(dis)\n",
    "        return knn_index[:self.k]\n",
    "    def get_label(self,x):\n",
    "        knn_index = self.get_knn_index(x)\n",
    "        label_stat = np.zeros(10,dtype =int)\n",
    "        for index in knn_index:\n",
    "            label = y_train[index]\n",
    "            label_stat[label] += 1\n",
    "        return np.argmax(label_stat)\n",
    "    def predict(self,x_test,y_test):\n",
    "        y_predict_list = []\n",
    "        for x in x_test:\n",
    "            y_pred = self.get_label(x)\n",
    "            y_predict_list.append(y_pred)\n",
    "        return np.mean(y_predict_list==y_test)\n",
    "\n",
    "        \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "bcb4d5c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "k: 1  acc: 0.88\n",
      "k: 2  acc: 0.88\n",
      "k: 3  acc: 0.87\n",
      "k: 4  acc: 0.895\n",
      "k: 5  acc: 0.87\n",
      "k: 6  acc: 0.885\n",
      "k: 7  acc: 0.88\n",
      "k: 8  acc: 0.87\n",
      "k: 9  acc: 0.87\n"
     ]
    }
   ],
   "source": [
    "for k in range(1,10):\n",
    "    knn = KNN(k)\n",
    "    knn.fit(x_train,y_train)\n",
    "    print('k:',k,\" acc:\",knn.predict(x_test,y_test))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "younger",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.23"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
